<?php

namespace App\Services;
use App\Models\Order;
use App\Services\EasyWeChatServices;

class OrderServices
{
    public function get_order_list($user_id){
        $order = Order::where('user_id',$user_id)
           ->orderBy('id','desc')
            ->where('order_status',2)
            ->simplePaginate(10);
        return $order;
    }

    public function my_order_info($user_id){
        $order = Order::where('user_id',$user_id)
            ->first();
        return $order;
    }

    public function pay_order($order_id,$open_id,$notify_url){
        $order = Order::where('id',$order_id)->first();
        $app = EasyWeChatServices::pay_app();
        $result = $app->order->unify([
            'body' => '换购',
            'out_trade_no' =>$order->order_number,
            'total_fee' => $order->order_price * 100,
           // 'spbill_create_ip' => '123.12.12.123', // 可选，如不传该参数，SDK 将会自动获取相应 IP 地址
            'notify_url' => $notify_url, // 支付结果通知网址，如果不设置则会使用配置里的默认地址
            'trade_type' => 'JSAPI', // 请对应换成你的支付方式对应的值类型
            'openid' => $open_id,
        ]);
        //dd($result);
        if ($result['result_code'] == 'SUCCESS' && $result['return_code'] == 'SUCCESS') {
            $prepayId = $result['prepay_id'];
            $jssdk = $app->jssdk;
            $config = $jssdk->sdkConfig($prepayId);
            return $config;
        } else {
            return false;
        }
    }
}